package com.sunfield.windpower.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunfield.windpower.R;
import com.sunfield.windpower.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxl on 2017/2/7.
 */

public class MyTabLayout extends HorizontalScrollView implements ViewPager.OnPageChangeListener, View.OnClickListener {
    LinearLayout ll_tab;
    ImageView iv_tab_line;
    ViewPager viewPager;

    List<TextView> tabs;

    int size;

    int lineLength;
    int offset;
    int tabWidth;

    int tabColorSelected;
    int tabColorNormal;

    List<String> titleList;

    public MyTabLayout(Context context) {
        super(context);
        init();
    }

    public MyTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.view_tablayout, this, true);
        ll_tab = (LinearLayout) findViewById(R.id.ll_tab);
        iv_tab_line = (ImageView) findViewById(R.id.iv_tab_line);

        tabs = new ArrayList<>();
        setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
        setHorizontalScrollBarEnabled(false);

        lineLength = getResources().getDimensionPixelOffset(R.dimen.tab_line_width_4);
        tabColorSelected = Color.BLACK;
        tabColorNormal = Color.GRAY;
    }

    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
        tabWidth = lineLength + offset * 2;
        PagerAdapter adapter = viewPager.getAdapter();
        size = adapter.getCount();

        tabs.clear();
        ll_tab.removeAllViews();
        for(int i=0;i<size;i++){
            TextView tab = new TextView(getContext());
            if(titleList != null && !titleList.isEmpty()){
                tab.setText(titleList.get(i % titleList.size()));
            }
            tab.setTextSize(15);
            tab.setGravity(Gravity.CENTER);
            tab.setOnClickListener(this);
            tabs.add(tab);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(tabWidth, ViewGroup.LayoutParams.MATCH_PARENT);
            ll_tab.addView(tab, layoutParams);
        }

        initTab();

        viewPager.addOnPageChangeListener(this);
    }

    void initTab() {
        Bitmap oldBm = BitmapFactory.decodeResource(getResources(), R.drawable.tab_line);
        Bitmap line = Bitmap.createScaledBitmap(oldBm, lineLength, oldBm.getHeight(), false);
//        lineLength = oldBm.getWidth();

        iv_tab_line.setImageBitmap(line);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        Matrix matrix = new Matrix();
        float dx = lineLength * (position + positionOffset) + offset * ((position + positionOffset) * 2 + 1);
        matrix.postTranslate(dx, 0);
        iv_tab_line.setImageMatrix(matrix);
        for (int i = 0; i < tabs.size(); i++) {
            TextView tab = tabs.get(i);
            if (i == position) {
                tab.setTextColor(Tools.getColorEvaluate(tabColorSelected, tabColorNormal, positionOffset));
            } else if (i == position + 1) {
                tab.setTextColor(Tools.getColorEvaluate(tabColorNormal, tabColorSelected, positionOffset));
            } else {
                tab.setTextColor(tabColorNormal);
            }
        }
    }

    @Override
    public void onPageSelected(int position) {
        smoothScrollTo((position - 1) * tabWidth, 0);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    public void setLineLength(int lineLength) {
        if (lineLength > 0) {
            this.lineLength = lineLength;
        }
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public void setTabColorSelected(int tabColorSelected) {
        this.tabColorSelected = tabColorSelected;
    }

    public void setTabColorNormal(int tabColorNormal) {
        this.tabColorNormal = tabColorNormal;
    }

    public void setTitleList(List<String> titleList) {
        this.titleList = titleList;
    }

    @Override
    public void onClick(View v) {
        int index = tabs.indexOf(v);
        viewPager.setCurrentItem(index, true);
    }
}
